﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using MyWeb.Business;

namespace MyWeb.Modules.Products
{
    public partial class Shopcart : System.Web.UI.Page
    {
        private string ProductsId = "";
        private string Quantity = "";
        DataTable dtCard;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Quantity"] == null)
                {
                    this.lblmsg.Text = "Hiện không có sản phẩm nào trong giỏ hàng !";
                }
                if (Session["Quantity"] != null)
                {
                    Quantity = Session["Quantity"].ToString();
                    lblmsg.Visible = false;
                }
                if (Session["ProductsId"] != null)
                {
                    ProductsId = Session["ProductsId"].ToString();
                    if (CheckProductsId(ProductsId) == true)
                    {
                        DataTable dt = (DataTable)Session["Card"];
                        rptshopcart.DataSource = dt;
                        rptshopcart.DataBind();
                    }
                    else
                    {
                        ShowShopcard();
                    }
                    for (int i = 0; i < rptshopcart.Items.Count; i++)
                    {
                        Label lblproId = (Label)rptshopcart.Items[i].FindControl("lblId");
                        TextBox txtquantity = (TextBox)rptshopcart.Items[i].FindControl("txtquantity");
                        DataTable dt1 = (DataTable)Session["Card"];
                        if (lblproId.Text.ToString().Equals(dt1.Rows[i]["Id"].ToString()))
                        {
                            txtquantity.Text = dt1.Rows[i]["Quantity"].ToString();
                        }
                    }
                }
                else
                {
                    dtCard = (DataTable)Session["Card"];
                    rptshopcart.DataSource = dtCard;
                    rptshopcart.DataBind();
                }
                ShowTotal();
            }

        }
        private void ShowShopcard()
        {
            if (Session["Card"] == null)
            {
                dtCard = new DataTable();
                dtCard.Columns.Add("Id", typeof(String));
                dtCard.Columns.Add("Name", typeof(String));
                dtCard.Columns.Add("Quantity", typeof(String));
                dtCard.Columns.Add("Price", typeof(String));
                dtCard.Columns.Add("Money", typeof(String));
                List<Data.Products> list = ProductsService.Products_GetById(ProductsId);
                var listPrice = PackProductsService.PackProducts_GetByTop("", "ProductsId = '"+ list[0].Id +"'", "");
                DataRow rows = dtCard.NewRow();
                rows["Id"] = list[0].Id;
                rows["Name"] = list[0].Name;
                rows["Quantity"] = Quantity;
                rows["Price"] = listPrice[0].Price;
                string PricePro = rows["Price"].ToString();
                rows["Money"] = (Int32.Parse(PricePro) * Int32.Parse(Quantity)).ToString();
                dtCard.Rows.Add(rows);
            }
            else
            {
                dtCard = (DataTable)Session["Card"];
                List<Data.Products> listpro = ProductsService.Products_GetById(ProductsId);
                DataRow rows = dtCard.NewRow();
                rows["Id"] = listpro[0].Id;
                rows["Name"] = listpro[0].Name;
                rows["Quantity"] = Quantity;
                var listPrice = PackProductsService.PackProducts_GetByTop("", "ProductsId = '" + listpro[0].Id + "'", "");
                string Pricepro = rows["Price"].ToString();
                rows["Money"] = (Int32.Parse(Pricepro) * Int32.Parse(Quantity)).ToString();
                dtCard.Rows.Add(rows);
            }
            Session["Card"] = dtCard;
            DataTable dt = (DataTable)Session["Card"];
            rptshopcart.DataSource = dt;
            rptshopcart.DataBind();
        }
        private void ShowTotal()
        {
            int total = 0;
            foreach (RepeaterItem item in this.rptshopcart.Items)
            {
                Label lbl = (Label)item.FindControl("lblMoney");
                lbl.Text = lbl.Text.Replace(".", "");
                total += Int32.Parse(lbl.Text);
                lbl.Text = Format_Price(lbl.Text);
            }
            lblTotal.Text = Format_Price(total.ToString()) + " VNĐ";
            Session["TotalPrice"] = total;
        }

        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
        private bool CheckProductsId(string productid)
        {
            bool flag = false;
            if (Session["Card"] != null)
            {
                DataTable dtcheck = (DataTable)Session["Card"];
                if (dtcheck.Rows.Count > 0)
                {
                    for (int i = 0; i < dtcheck.Rows.Count; i++)
                    {
                        if (dtcheck.Rows[i]["Id"].Equals(productid))
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
                else
                {
                    flag = false;
                }
            }
            return flag;
        }
        protected void rptshopcart_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Update":
                    Label lblProId;
                    string lbltotal;
                    foreach (RepeaterItem item in this.rptshopcart.Items)
                    {
                        lblProId = (Label)item.FindControl("lblId");
                        if (lblProId.Text.Trim().Equals(strCA))
                        {
                            Label lblPrice = (Label)item.FindControl("lblPrice");
                            TextBox txtquantity = (TextBox)item.FindControl("txtquantity");
                            string number = txtquantity.Text;
                            string gia = lblPrice.Text.Replace(".", "");
                            lbltotal = (Int32.Parse(gia) * Int32.Parse(number)).ToString();
                            CardUpdateNumber((DataTable)Session["Card"], strCA, Int32.Parse(number), lbltotal);
                        }
                    }
                    Response.Redirect(Request.Url.ToString());
                    break;
                case "Delete":
                    dtCard = (DataTable)Session["Card"];
                    if (dtCard.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtCard.Rows.Count; i++)
                        {
                            if (dtCard.Rows[i]["Id"].ToString().Equals(strCA))
                            {
                                dtCard.Rows[i].Delete();
                                Session.Remove("ProductsId");
                            }
                        }
                    }
                    Session["Card"] = dtCard;
                    rptshopcart.DataSource = dtCard;
                    rptshopcart.DataBind();
                    Response.Redirect(Request.Url.ToString());
                    break;
            }
        }
        private void CardUpdateNumber(DataTable Card, string proid, int quantity, string Total)
        {
            if (Card.Rows.Count > 0)
            {
                for (int i = 0; i < Card.Rows.Count; i++)
                {
                    if (Card.Rows[i]["Id"].ToString().Equals(proid))
                    {
                        Card.Rows[i]["Quantity"] = quantity;
                        Card.Rows[i]["Money"] = Total;
                        break;
                    }
                }
            }
        }

        protected void lbtback_Click(object sender, EventArgs e)
        {
            Response.Redirect("/");
        }

        protected void lbtpayT_Click(object sender, EventArgs e)
        {
            if (Session["CusId"] != null)
            {
                int sstotalprice = 0;
                foreach (RepeaterItem item in this.rptshopcart.Items)
                {
                    Label lblTotalPrice = (Label)item.FindControl("lblPrice");
                    lblTotalPrice.Text = lblTotalPrice.Text.Replace(".", "");
                    sstotalprice += Int32.Parse(lblTotalPrice.Text);
                }
                Session["TotalPrice"] = sstotalprice.ToString();
            }
            else
            {
                Response.Redirect("/logon.html");
            }
        }
    }
}